package swd.work.conf_form_fields;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;
import bsh.Interpreter;

public class Action {
    QueryBean gqbnDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        view.setLoginUserNO(request.getSession().getAttribute("u_id").toString());
        gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());

        DataView DataView = null;
        DataViewForm DataViewForm = null;

        DataView = new DataView(gqbnDAO);
        DataViewForm = new DataViewForm();

        request.setAttribute("DataViewForm", DataViewForm);//讓前端可以呼叫dataview

        view.setDataViewForm(DataViewForm);
        view.setDateView(DataView);

        Business business = new Business(gqbnDAO);

        String lstrResult = "";

        Interpreter lbhsIPR = new Interpreter();//可動態執行字串中的程式碼物件
        StringBuffer lstbDynamicCode=new StringBuffer();

        try {
            //需先定義字串中的物件變數
            lbhsIPR.set("business",business);
            lbhsIPR.set("view", view);

            if(view.getPrgAction()!=null && view.getPrgAction().equals("getData")){//利用AJAX即時讀取資料
                lstrResult=business.getData(view);

                pageContext.getOut().clear();
                pageContext.getOut().write(lstrResult);
                pageContext.getOut().flush();
                return "login";
             }

             //每次submit一開始都要做的事
             lstrResult = business.actionForInit(view);

             //執行各動作對應的method
             if(view.getPrgAction()!=null){
                 lstbDynamicCode.append("business.actionFor");
                 lstbDynamicCode.append(view.getPrgAction().substring(0,1).toUpperCase());
                 lstbDynamicCode.append(view.getPrgAction().substring(1));
                 lstbDynamicCode.append("(view);");
                 //執行字串中的動態程式碼
                 lstrResult=lbhsIPR.eval(lstbDynamicCode.toString()).toString();
             }

             lstrResult = business.actionForEnd(view);

             business.setButtonShow(view,view.getPrgAction(),view.getPrgStatus());
             business.setReadonlyFields(view,view.getPrgAction(),view.getPrgStatus());
             business.setShowFields(view,view.getPrgAction(),view.getPrgStatus());

             return "";
      }
        catch (Exception e) {
            view.setOnloadScript("alert('Program error please contact IT.');");
            e.printStackTrace();
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
